package com.xmkj.carwash.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.carbean.MessageListBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.DeleteMsgMethods;
import com.common.retrofit.methods.GetMessageListMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.common.widget.recyclerview.refresh.recycleview.swipemenu.SwipeMenuRecycler;
import com.xmkj.carwash.R;
import com.xmkj.carwash.adapter.MessageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chen on 2018/3/28.
 */

public class MessageListActivity extends BaseMvpActivity {
    @BindView(R.id.recyclerview)
    SwipeMenuRecycler recyclerview;
    private MessageAdapter adapter;
    List<MessageListBean.ListsEntity> bean = new ArrayList<>();

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        gotoHttpRep();
//        mPageSize = 12;
        setRecyclerView();
    }

    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setLoadMoreEnabled(true);
        recyclerview.setRefreshing(true);
//        recyclerview.setRefreshEnabled(false);

        adapter = new MessageAdapter(context, bean);

        adapter.setOnItemChildClickListener(new CommonAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(CommonAdapter adapter, View view, int position) {
                MessageListBean.ListsEntity bean = (MessageListBean.ListsEntity) adapter.mData.get(position);
                if (view.getId() == R.id.btn_delete) {
                    //删除购物车6c29a76295f1f10e88e67dd66770f037
                    deleteShopCar(position, bean.id);
                }
            }
        });

        recyclerview.setAdapter(adapter);

        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }

    private void deleteShopCar(final int position, int id) {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                adapter.remove(position);
                showToastMsg("删除成功");

//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        DeleteMsgMethods.getInstance().delData(subscriber, uid, hashid, id + "");
        rxManager.add(subscriber);
    }

    private void gotoHttpRep() {
        CommonSubscriber<MessageListBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                MessageListBean beans = (MessageListBean) o;
                List<MessageListBean.ListsEntity> beanList = beans.lists;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetMessageListMethods.getInstance().listData(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID), mPageIndex, "");
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("消息");
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
